package com.androwit.engine.mapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.androwit.engine.MappingReader;

/**
 * A simple in memory sample of a {@link MappingReader}
 * 
 * @author Zied Hamdi Founder of http://1vu.fr, hamdi
 */
public class InMemoryMappingReader implements MappingReader<Integer, String> {
	List<Map<Integer, String>> rowValues = new ArrayList<Map<Integer, String>>();
	int currentIndex = 0;
	Map<Integer, String> currentRow;

	void newLine(String... values) {
		Map<Integer, String> row = new HashMap<Integer, String>();
		for (int i = 0; i < values.length; i++) {
			row.put(i, values[i]);
		}
		rowValues.add(row);
	}

	@Override
	public String get(MappingField<?, Integer, String> field) {
		return currentRow.get(field.getKey());
	}

	/**
	 * we return no value since the fields are the same for each line
	 * 
	 * @see com.androwit.engine.MappingReader#getRelevantValues()
	 */
	public Map<Integer, String> getRelevantValues(
			MappingFieldsDefiner<?, Integer, String> fieldsDefiner) {
		return null;
	}

	@Override
	public boolean readRecord() {
		if (currentIndex == rowValues.size())
			return false;

		currentRow = rowValues.get(currentIndex);
		currentIndex++;
		return true;
	}

}